package xxl.robotlegs.extensions.modularity.impl 
{
	import xxl.robotlegs.extensions.contextsignal.api.IContextSignal;
	import xxl.robotlegs.extensions.contextsignal.impl.ContextSignalRelay;
	import xxl.robotlegs.extensions.modularity.api.IModuleConnectionAction;
	/**
	 * ...
	 * @author XXL
	 */
	public class ModuleConnectionConfigurator implements IModuleConnectionAction
	{
		private var _channelToLocalRelay:ContextSignalRelay;
		private var _localToChannelRelay:ContextSignalRelay;
		
		public function ModuleConnectionConfigurator(localSignal:IContextSignal, channelSignal:IContextSignal) 
		{
			_localToChannelRelay = new ContextSignalRelay(localSignal, channelSignal).start();
			_channelToLocalRelay = new ContextSignalRelay(channelSignal, localSignal).start();
		}
		
		public function destroy():void
		{
			_localToChannelRelay.stop();
			_localToChannelRelay = null;
			_channelToLocalRelay.stop();
			_channelToLocalRelay = null;
		}
		
		//----------private
		
		private function reportError(msg:String):void
		{
			throw new Error(msg);
		}
		
		/* INTERFACE xxl.robotlegs.extensions.modularity.api.IModuleConnectionAction */
		
		public function relaySignal(key:Object):IModuleConnectionAction 
		{
			_channelToLocalRelay.hasKey(key) && reportError("已经在接收Key为 " + key + " 的信号了，不能再转发了，否会导致死循环");
			_localToChannelRelay.addKey(key);
			return this;
		}
		
		public function receiveSignal(key:Object):IModuleConnectionAction 
		{
			_localToChannelRelay.hasKey(key) && reportError("已经在转发Key为 " + key + " 的信号了，不能再接收了，否会导致死循环");
			_channelToLocalRelay.addKey(key);
			return this;
		}
		
		public function suspend():void 
		{
			_channelToLocalRelay.stop();
			_localToChannelRelay.stop();
		}
		
		public function resume():void 
		{
			_channelToLocalRelay.start();
			_localToChannelRelay.start();
		}
		
	}

}